import mongoose, { Document, Schema } from 'mongoose'

export interface INotificationTemplate extends Document {
  code: string // 模板代码
  type: 'reward' | 'system' | 'follow' | 'gift'
  title: string // 标题模板
  content: string // 内容模板
  variables: string[] // 变量列表
  enabled: boolean
  createdAt: Date
  updatedAt: Date
}

const NotificationTemplateSchema = new Schema({
  code: {
    type: String,
    required: true,
    unique: true,
    trim: true,
  },
  type: {
    type: String,
    enum: ['reward', 'system', 'follow', 'gift'],
    required: true,
  },
  title: {
    type: String,
    required: true,
  },
  content: {
    type: String,
    required: true,
  },
  variables: [
    {
      type: String,
      trim: true,
    },
  ],
  enabled: {
    type: Boolean,
    default: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
  updatedAt: {
    type: Date,
    default: Date.now,
  },
})

// 更新时间中间件
NotificationTemplateSchema.pre('save', function (next) {
  this.updatedAt = new Date()
  next()
})

export const NotificationTemplate = mongoose.model<INotificationTemplate>(
  'NotificationTemplate',
  NotificationTemplateSchema
)
